﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using ChordAnalizer;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            listBoxSearch.Items.Clear();
            ChordAnalizer.ChordAnalizerComponent komponen = new ChordAnalizerComponent();
            string keywords = txtInput.Text;
            List<string> hasil = new List<string>();

            hasil = komponen.getURLbyTitle(txtInput.Text);
            for (int i = 0; i < hasil.Count; i++)
            {
                listBoxSearch.Items.Add(hasil[i]);
            }

            /*rtbLyric.Text = komponen.getLyricAndChord("http://tabs.ultimate-guitar.com/p/peterpan/semua_tentang_kita_crd.htm");

            string[] stringHasil = rtbLyric.Text.Split('\n');

            int count = stringHasil.Length;
            int start = 0;
            for (int i = 0; i < count; i++)
            {
                if (stringHasil[i].Length <= 1) continue;

                if ((stringHasil[i][1] == ' ' && stringHasil[i][2] == ' ') ||
                    (stringHasil[i][2] == ' ' && stringHasil[i][3] == ' '))
                {
                    start = i; break;
                }
            }
            for (int i = start; i < count; i++)
            {
                richTextBox1.AppendText(i + " " + stringHasil[i] + "\n");
            }*/

            //if (cmbCategory.SelectedItem.ToString() == "Title")
            //{
            //    hasil = komponen.getURLbyTitle(keywords);
            //    //MessageBox.Show("Ini cari title");//ini berhasil kog

            //}
            //else if (cmbCategory.SelectedItem.ToString() == "Artist")
            //{
            //    hasil = komponen.getURLbyArtist(keywords);
            //}

            //for (int i = 0; i < hasil.Count; i++)
            //{
            //    listBoxSearch.Items.Add(hasil[i]);
            //}


            //listViewChord.Clear();
            //imageList1 = new ImageList();

            //string getUrl = komponen.getChordOnly(keywords);

            //System.Net.WebRequest request = System.Net.WebRequest.Create(getUrl);
            //System.Net.WebResponse response = request.GetResponse();
            //System.IO.Stream responseStream = response.GetResponseStream();
            //Bitmap bmp = new Bitmap(responseStream);
            //responseStream.Dispose();

            //Bitmap getImg = new Bitmap();
            //getImg = (Bitmap) komponen.getChordOnly(keywords);
            //Bitmap getImg = komponen.getChordOnly(keywords);
            
            //imageList1.Images.Add(getImg);
            //listViewChord.Show();

            //pictureBoxChord.ImageLocation = komponen.getChordOnly(keywords);
            //pictureBoxChord.Show();
        }

        private void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void listBoxSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            rtbLyric.Text = "";
            string input = listBoxSearch.SelectedItem as string;

            ChordAnalizerComponent komponen = new ChordAnalizerComponent();

            string[] stringHasil = input.Split('~');
            komponen.getLyricAndChord(stringHasil[2]);

            int count = ChordAnalizerComponent.listStringHasil.Count;
            richTextBox1.Text = String.Empty;
            for (int i = 0; i < count; i++)
            {
                rtbLyric.AppendText(WebUtility.HtmlDecode(ChordAnalizerComponent.listStringHasil[i].Item1));
                if (ChordAnalizerComponent.listStringHasil[i].Item2)
                    richTextBox1.AppendText(ChordAnalizerComponent.listStringHasil[i].Item1 + "\n");
            }

            /*string[] stringHasil2 = rtbLyric.Text.Split('\n');

            int start = 0;
            for (int i = 0; i < count; i++)
            {
                if (stringHasil2[i].Length <= 1) continue;

                if ((stringHasil2[i][1] == ' ' && stringHasil2[i][2] == ' ') ||
                    (stringHasil2[i][2] == ' ' && stringHasil2[i][3] == ' '))
                {
                    start = i; break;
                }
            }

            richTextBox1.Text = "";
            for (int i = start; i < count; i++)
            {
                richTextBox1.AppendText(i + " " + stringHasil2[i] + "\n");
            }*/

            //string showLyric = komponen.getLyricAndChord(listBoxSearch.SelectedItem.ToString());
            //rtbLyric.Text = showLyric;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDocument doc = new PrintDocument();
            PrintDialog pd = new PrintDialog();
            PrintPreviewDialog ppd = new PrintPreviewDialog();
            ppd.Document = doc;
            pd.Document = doc;
            doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
            if (ppd.ShowDialog() == DialogResult.OK)
            {
                if (pd.ShowDialog() == DialogResult.OK)
                {
                    doc.Print();
                }
            }
        }

        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            int x = 10;
            int y = 0;
            int charpos = 0;
            while (charpos < rtbLyric.Text.Length)
            {
                if (rtbLyric.Text[charpos] == '\n')
                {
                    charpos++;
                    y += 20;
                    x = 10;
                }
                else if (rtbLyric.Text[charpos] == '\r')
                {
                    charpos++;
                }
                else
                {
                    rtbLyric.Select(charpos, 1);
                    e.Graphics.DrawString(rtbLyric.SelectedText, rtbLyric.SelectionFont, new SolidBrush(rtbLyric.SelectionColor), new PointF(x, y));
                    x = x + 8;
                    charpos++;
                }
            }
        }

        private void btnSearchChord_Click(object sender, EventArgs e)
        {
            ChordAnalizer.ChordAnalizerComponent komponen = new ChordAnalizerComponent();
            string keywords = txtChord.Text;

            pictureBoxChord.ImageLocation = komponen.getChordOnly(keywords);
            pictureBoxChord.Show();
        }

        private void btnTranspose_Click(object sender, EventArgs e)
        {
            //string keywords = "null";
            //ChordComponent.KomponenChord komponen2 = new ChordComponent.KomponenChord();
            ChordAnalizer.ChordAnalizerComponent komponen = new ChordAnalizerComponent();
            //keywords = txtBoxChordTranspose.Text;
            string keywords = txtBoxChordTranspose.Text;
            int tampung = int.Parse(txtBoxTmp.Text);
            //lblResult.Text = komponen.transpose(keywords);
            for (int i = 0; i < tampung; i++)
            {
                keywords = komponen.transpose(keywords);
            }
            lblResult.Text = keywords;
        }


    }
}
